/**
 * @Title: BlogController.java
 * @Package com.zll.ccp.controller.blog
 * @author zhanglinlu
 * @date 2019年5月4日 下午3:30:59
 */
package com.zll.ccp.controller.blog;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zll.ccp.annotation.RequireOwn;
import com.zll.ccp.model.pojo.ResourceType;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zll.ccp.constants.Constant;
import com.zll.ccp.model.blog.Blog;
import com.zll.ccp.service.blog.IBlogService;
import com.zll.ccp.utils.CommonUtils;

/**
 * @ClassName: BlogController
 * @Description: 博客controller
 * @author zhanglinlu
 * @date 2019年5月4日 下午3:30:59
 * 
 */
@Controller
public class BlogController {
    @Autowired
    private IBlogService blogService;

    /**
     * @Title: editBlog
     * @Description: 去编辑博客页面
     * @param id 主键id
     * @return
     */
    //@RequireOwn(resourceType = ResourceType.blog,paramName = "id")
    @RequestMapping("/editblog")
    public String editBlog(String id, HttpServletRequest request) {
        request.setAttribute("blog", blogService.getBlogById(id));
        request.setAttribute("blogTypeList", blogService.getBlogTypeList());
        return "blog/editBlog";
    }

    /**
     * @Title: submitBlog
     * @Description: 添加或修改博客发布
     * @param blog 博客
     * @return 添加结果
     */
    @PostMapping("/submitblog")
    @ResponseBody
    public Object submitBlog(@RequestBody Blog blog, HttpSession session) {
        JSONObject result = new JSONObject();
        // 为空则新增
        if (StringUtils.isBlank(blog.getId())) {
            String id = CommonUtils.getUUID();
            blog.setId(id);// 设置主键
            blog.setCreateTime(CommonUtils.getNowDate());// 创建时间
            blog.setUserId(session.getAttribute("userId").toString());
            blogService.addBlog(blog);
            result.put("isAdd", true);
            result.put("id", id);
            return result;
        }
        // 不为空则修改
        //增加判断权限
        String userId = session.getAttribute("userId").toString();
        if (!StringUtils.equals(userId, blog.getUserId())) {
            throw new UnauthorizedException();
        }
        blogService.updateBlog(blog);
        result.put("isAdd", false);
        return result;
    }

    /**
     * @Title: blogImageUpload
     * @Description: 文章编辑器上传图片
     * @param blogimage 图片
     * @return 上传结果
     */
    @RequestMapping("/blogimageupload")
    @ResponseBody
    public Object blogImageUpload(MultipartFile blogimage) {
        JSONObject result = new JSONObject();
        result.put("error", 0);
        String originalFilename = blogimage.getOriginalFilename();
        String fileType = StringUtils.split(originalFilename, ".")[1];
        // 存储后文件的新名字
        String newName = UUID.randomUUID().toString().replace("-", "") + "." + fileType;
        String path = Constant.NGINX_IMAGES_PATH + newName;
        File savedFile = new File(path);
        try {
            boolean isCreateSuccess = savedFile.createNewFile();
            if (isCreateSuccess) {
                blogimage.transferTo(savedFile);
            }
        } catch (IOException e) {
            result.put("error", 1);
        }
        if (result.getIntValue("error") == 0) {// 图片转化成功的话
            // 如果转化成功url的地址
            String imagePath = Constant.NGINX_INTERNET_PRE + newName;
            JSONArray images = new JSONArray();
            images.add(imagePath);
            result.put("data", images);
        }
        return result;
    }

}
